from pyquery import PyQuery
from requests.exceptions import RequestException
import requests
import time,json
from urllib.request import urlretrieve

index = 1

def getPage(url):
    '''爬取数据'''
    try:
        headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.26 Safari/537.36 Core/1.63.5383.400 QQBrowser/10.0.1313.400',
                 }
        data = requests.get(url,headers=headers)
        if data.status_code == 200:
            return data.json()
        else:
            return None
    except RequestException as err:
        print(err)
        return None

def parseData(content):
    '''解析数据'''
    list = content['data']
    for item in list:
        yield item

def saveFile(item):
    try:
        url = item['thumbURL']
        urlretrieve(item['thumbURL'],'./pic/' + item['di']+'.jpg')
    except Exception as err:
        print(err)

def start(gsm):
    global index
    url = 'https://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&is=&fp=result&queryWord=%E9%92%A2%E9%93%81%E4%BE%A0&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=-1&z=&ic=0&word=%E9%92%A2%E9%93%81%E4%BE%A0&s=&se=&tab=&width=&height=&face=0&istype=2&qc=&nc=1&fr=&pn=' + str(30*index) + '&rn=30&gsm=' + gsm
    htmlJson = getPage(url)
    if htmlJson:
        nextGsm = htmlJson['gsm']
        items = parseData(htmlJson)
        for item in items:
            saveFile(item)
        index += 1
        print(nextGsm)
        start(nextGsm)


    '''爬虫入口'''
if __name__ == '__main__':
    start('1e')
